home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 14 / Example 14.1 / action.cpp next >
Encoding:
C/C++ Source or Header  |  2006-06-30  |  2.6 KB  |  94 lines

  1. #include "action.h"
  2. #include "player.h"
  3. #include "strategyMap.h"
  4.  
  5. //////////////////////////////////////////////////////////////////////////////////////////////
  6. //                                TRAIN UNIT                                                    //
  7. //////////////////////////////////////////////////////////////////////////////////////////////
  8.  
  9. TRAIN_UNIT::TRAIN_UNIT(int unitType)
  10. {
  11.     m_unitToTrain = unitType;
  12. }
  13.  
  14. AI_ACTION* TRAIN_UNIT::RequiresAction(PLAYER *player)
  15. {
  16.     //Check that the needed building exist
  17.     if(m_unitToTrain == WORKER)
  18.     {
  19.         if(!player->HasMapObject(TOWNHALL, true))
  20.         {
  21.             if(player->HasMapObject(WORKER, false))
  22.                 return new CONSTRUCT_BUILDING(TOWNHALL);
  23.             else return NULL;
  24.         }
  25.     }
  26.     else if(m_unitToTrain == SOLDIER)
  27.     {
  28.         if(!player->HasMapObject(BARRACKS, true))
  29.             return new CONSTRUCT_BUILDING(BARRACKS);
  30.     }
  31.     else if(m_unitToTrain == MAGICIAN)
  32.     {
  33.         if(!player->HasMapObject(TOWER, true))
  34.             return new CONSTRUCT_BUILDING(TOWER);
  35.     }
  36.  
  37.     return NULL;
  38. }
  39.  
  40. void TRAIN_UNIT::PerformAction(PLAYER *player)
  41. {
  42.     //Check that the player has cash to train the unit
  43.     if(player->money < GetCost(m_unitToTrain, false))return;
  44.  
  45.     //Get an available building
  46.     BUILDING *building = player->GetAvailableBuilding(m_unitToTrain);
  47.     if(building == NULL)return;
  48.  
  49.     //Start training the unit
  50.     building->TrainUnit(m_unitToTrain);
  51. }
  52.  
  53. //////////////////////////////////////////////////////////////////////////////////////////////
  54. //                                CONSTRUCT BUILDING                                            //
  55. //////////////////////////////////////////////////////////////////////////////////////////////
  56.  
  57. CONSTRUCT_BUILDING::CONSTRUCT_BUILDING(int buildingType)
  58. {
  59.     m_buildingToMake = buildingType;
  60. }
  61.  
  62. AI_ACTION* CONSTRUCT_BUILDING::RequiresAction(PLAYER *player)
  63. {
  64.     //Check that we have a worker
  65.     if(!player->HasMapObject(WORKER, false))
  66.         return new TRAIN_UNIT(WORKER);
  67.  
  68.     //Check that we have all prerequisite buildings
  69.     if(m_buildingToMake == BARRACKS || m_buildingToMake == TOWER)
  70.         if(!player->HasMapObject(TOWNHALL, true))
  71.             return new CONSTRUCT_BUILDING(TOWNHALL);
  72.  
  73.     if(m_buildingToMake == TOWER)
  74.         if(!player->HasMapObject(BARRACKS, true))
  75.             return new CONSTRUCT_BUILDING(BARRACKS);
  76.  
  77.     return NULL;
  78. }
  79.  
  80. void CONSTRUCT_BUILDING::PerformAction(PLAYER *player)
  81. {
  82.     //Check that the player has cash to train the unit
  83.     if(player->money < GetCost(m_buildingToMake, true))return;
  84.  
  85.     //Get Building position
  86.     INTPOINT buildPos = player->FindClosestBuildingLocation(m_buildingToMake, player->m_teamStartLocation);
  87.  
  88.     //Get Available worker
  89.     UNIT *worker = player->GetAvailableUnit(WORKER);
  90.     if(worker == NULL)return;
  91.  
  92.     //Build building
  93.     worker->ConstructBuilding(m_buildingToMake, buildPos);
  94. }